package com.syt.generator;

import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;

/**
 * @description: 读取yml
 * @create: 2022-05-16 21:18
 **/
@Component
public class YmlUtils {

    public static String getString(LinkedHashMap<String, Object> sourceMap, String key) {
        String[] keys = key.split("[.]");
        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) sourceMap.clone();
        int length = keys.length;
        Object resultValue = null;
        for (int i = 0; i < length; i++) {
            Object value = map.get(keys[i]);
            if (i < length - 1) {
                map = ((LinkedHashMap<String, Object>) value);
            } else if (value == null) {
                throw new RuntimeException("key is not exists!");
            } else {
                resultValue = value;
            }
        }
        return resultValue.toString();
    }

//    public static void main(String[] args) {
//        Yaml yaml = new Yaml();
//        InputStream in = YmlUtils.class.getResourceAsStream("/application.yml");
//        LinkedHashMap<String, Object> sourceMap = (LinkedHashMap<String, Object>) yaml.load(in);
//        String directoryName = getString(sourceMap, "tensquare.directoryName");
//        String packageName = getString(sourceMap, "tensquare.packageName");
//        System.out.println(directoryName);
//        System.out.println(packageName);
//    }
}
